# -*- coding:utf-8 -*-

# @Time    : 2018/10/11 3:19 PM

# @Author  : Swing


# 数据读取以及基本处理
import pandas as pd
import numpy as np

# 可视化
import seaborn as sn
import matplotlib.pyplot as plt
# %matplotlib inline

source = pd.read_csv('files/day.csv')

# 丢掉一些不必要的特征

source = source.drop('casual',axis=1).drop('registered',axis=1)

# 丢掉一些不必要的特征
source = source.drop('instant',axis=1)
source = source.drop('dteday',axis=1) # 日期与输出y无关
source = source.drop('weekday',axis=1) # 节假日、工作日对输出y影响重要，weekday意义不大

# 两温度，相关性太强，取其一
source = source.drop('temp',axis=1)

# 删除日期
# source = source.drop('date', axis=1).drop('dayofyear', axis=1)

# 用于后续显示权重系数对应的特征
columns = source.columns

source.head()

# 分离输入特征x和输出y
y = source['cnt'].values
x = source.drop('cnt', axis=1)

#用于后续显示权重系数对应的特征
columns = x.columns

#将数据分割训练数据与测试数据
from sklearn.model_selection import train_test_split

# 随机采样20%的数据构建测试样本，其余作为训练样本
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=33, test_size=0.2)
x_train.shape

# 数据标准化
from sklearn.preprocessing import StandardScaler

# 分别初始化对特征和目标值的标准化器
ss_x = StandardScaler()
ss_y = StandardScaler()

# 分别对训练和测试数据的特征以及目标值进行标准化处理

x_num_features = ['atemp','hum','windspeed']

x_train_num = x_train[x_num_features]
x_test_num = x_test[x_num_features]

x_train_num_temp = ss_x.fit_transform(x_train_num)
x_text_num_temp = ss_x.transform(x_test_num)

x_train_num = pd.DataFrame(data=x_train_num_temp, columns=x_num_features, index=x_train_num.index)
x_test_num = pd.DataFrame(data=x_text_num_temp, columns=x_num_features, index=x_test_num.index)

x_train['atemp'] = x_train_num['atemp']
x_train['hum'] = x_train_num['hum']
x_train['windspeed'] = x_train_num['windspeed']

y_train_temp = ss_y.fit_transform(y_train.reshape(-1, 1))
y_test_temp = ss_y.fit_transform(y_test.reshape(-1, 1))
y_train = pd.DataFrame(data=y_train_temp, columns=['cnt'], index=y_train.index)
y_test = pd.DataFrame(data=y_test_temp, columns=['cnt'], index=y_test.index)



#对y做标准化不是必须
#对y标准化的好处是不同问题的w差异不太大，同时正则参数的范围也有限
y_train = ss_y.fit_transform(y_train.reshape(-1, 1))
y_test = ss_y.transform(y_test.reshape(-1, 1))

x_train.head(), y_train.head()